<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\UserTable;
use App\Admin\Repositories\Talent;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TalentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Talent(['user:id,nickname,avatar']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->column('id')->sortable();
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('company_name');
            $grid->column('contact_name');
            $grid->column('sex')
                 ->using([1 => '男',2 => '女'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('contact_phone');
            $grid->column('status')
                ->using([1 => '待跟进',2 => '已跟进'])
                ->label([1 => 'danger',2 => 'success']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('user.nickname','用户昵称')->width(3);
                $filter->like('company_name')->width(3);
                $filter->like('contact_name')->width(3);
                $filter->equal('contact_phone')->width(3);
                $filter->equal('sex')
                      ->select([1 => '男',2 => '女'])
                      ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Talent(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('company_name');
            $show->field('contact_name');
            $show->field('sex');
            $show->field('contact_phone');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Talent(['user:id,avatar,nickname']), function (Form $form) {
            $form->display('id');

            $form->image('user.avatar','用户头像')
                 ->disable();
            $form->display('user.nickname','用户昵称');
            $form->display('company_name');
            $form->display('contact_name');
            $form->display('sex')->customFormat(function ($value){
                 return $value == 1 ? '男' : '女';
            });
            $form->display('contact_phone');
            $form->radio('status')
                ->options([1 => '待跟进',2 => '已跟进'])
                ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
